package com.hzlh.service.impl;

import com.hzlh.dao.TTalentsBaseDao;
import com.hzlh.model.TTalentsBase;
import com.hzlh.service.TTalentsBaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * (TTalentsBase)表服务实现类
 *
 * @author makejava
 * @since 2021-06-06 15:18:31
 */
@Service("tTalentsBaseService")
public class TTalentsBaseServiceImpl implements TTalentsBaseService {
    @Resource
    private TTalentsBaseDao tTalentsBaseDao;

    /**
     * 通过ID查询单条数据
     *
     * @param authorid 主键
     * @return 实例对象
     */
    @Override
    public TTalentsBase queryById(Long authorid) {
        return this.tTalentsBaseDao.queryById(authorid);
    }

    @Override
    public List<TTalentsBase> selectList(TTalentsBase tTalentsBase) {
        return tTalentsBaseDao.queryAll(tTalentsBase);
    }

    @Override
    public List<Map<String, Object>> selectCountryCount(List<String> remarkGroup, List<String> agencyGroup, List<String> fieldGroup, String latestTitle) {
        return tTalentsBaseDao.selectCountryCount(remarkGroup, agencyGroup, fieldGroup, latestTitle);
    }

    @Override
    public List<Map<String, Object>> selectProvinceCount(List<String> remarkGroup, List<String> agencyGroup, List<String> fieldGroup, String latestTitle) {
        return tTalentsBaseDao.selectProvinceCount(remarkGroup, agencyGroup, fieldGroup, latestTitle);
    }

    @Override
    public List<Map<String, Object>> selectCityCount(List<String> remarkGroup, List<String> agencyGroup, List<String> fieldGroup, String latestTitle) {
        return tTalentsBaseDao.selectCityCount(remarkGroup, agencyGroup, fieldGroup, latestTitle);
    }

    @Override
    public Map<String, List<String>> getLabelList(TTalentsBase tTalentsBase) {
        //添加机构列表
        Map<String, List<String>> map = new HashMap<>();
        List<String> agencyGroup = tTalentsBaseDao.selectAgencyGroup(tTalentsBase);
        map.put("agencyGroup", agencyGroup);

//        //添加领域列表
//        List<TTalentsBase> tTalentsBases = tTalentsBaseDao.queryAll(tTalentsBase);
//        HashSet<String> fieldSet = new HashSet<>();
//        tTalentsBases.stream().map(TTalentsBase::getPublishedPaperAreas).forEach(v -> {
//            if (v != null) {
//                fieldSet.addAll(Arrays.asList(v.split(",")));
//            }
//        });
//        List<String> fieldGroup = new ArrayList<>(fieldSet);
//        map.put("fieldGroup", fieldGroup);
//        //添加人才标签
//        HashSet<String> remarkSet = new HashSet<>();
//        tTalentsBases.stream().map(TTalentsBase::getRemark).forEach(v -> {
//            if (v != null) {
//                remarkSet.addAll(Arrays.asList(v.split(",")));
//            }
//        });
//        List<String> remarkGroup = new ArrayList<>(remarkSet);
//        map.put("remarkGroup", remarkGroup);
        // 数据库查询最小的数量进行数据统计
        List<String> strings = tTalentsBaseDao.selectDomainList(tTalentsBase);
        HashSet<String> remarkSet = new HashSet<>();
        strings.stream().forEach(v -> {
            if (StringUtils.isNotEmpty(v)) {
                remarkSet.addAll(Arrays.asList(StringUtils.split(v,",")));
            }
        });
        List<String> resultList = new ArrayList<>(remarkSet);
        map.put("fieldGroup", resultList);
        map.put("remarkGroup", resultList);


//        // 读取文件[文件读取改为数据库读取]
//        try {
//            String tempFilePath = "/worldlocation.json";
//            InputStream input = TTalentsBaseServiceImpl.class.getResourceAsStream(tempFilePath);
//            byte[] bytes = new byte[0];
//            bytes = new byte[input.available()];
//            input.read(bytes);
//            String str = new String(bytes);
//            // 转化JSON
//            //将字符串加载到jsonObject对象中
//            JSONObject jsonObject = JSON.parseObject(str);
//            List<String> fieldGroup = (List<String>) jsonObject.get("fieldGroup");
//            map.put("fieldGroup", fieldGroup);
//            List<String> remarkGroup = (List<String>) jsonObject.get("remarkGroup");
//            map.put("remarkGroup", remarkGroup);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        return map;
    }

    @Override
    public Map<String, Object> talentAnalysis(TTalentsBase tTalentsBase) {
        Map<String, Object> map = tTalentsBaseDao.talentAnalysis(tTalentsBase);
//        Map<String, Object> map = new HashMap<>();
//        map.put("talentsTotal", tTalentsBases.size());
//        int papersNumSum = tTalentsBases.stream().mapToInt(TTalentsBase::getPapersNum).sum();
//        map.put("papersNumSum", papersNumSum);
//        int quoteNumSum = tTalentsBases.stream().mapToInt(TTalentsBase::getQuoteNum).sum();
//        map.put("quoteNumSum", quoteNumSum);

//        //添加领域列表
//        HashSet<String> fieldSet = new HashSet<>();
//        tTalentsBases.stream().map(TTalentsBase::getPublishedPaperAreas).forEach(v -> {
//            if (v != null) {
//                fieldSet.addAll(Arrays.asList(v.split(",")));
//            }
//        });
//        List<String> fieldGroup = new ArrayList<>(fieldSet);
//        map.put("fieldGroup", fieldGroup);
//        //添加人才标签
//        HashSet<String> remarkSet = new HashSet<>();
//        tTalentsBases.stream().map(TTalentsBase::getRemark).forEach(v -> {
//            if (v != null) {
//                remarkSet.addAll(Arrays.asList(v.split(",")));
//            }
//        });
//        List<String> remarkGroup = new ArrayList<>(remarkSet);
//        map.put("remarkGroup", remarkGroup);
        List<String> strings = tTalentsBaseDao.selectDomainList(tTalentsBase);
        HashSet<String> remarkSet = new HashSet<>();
        strings.stream().forEach(v -> {
            if (StringUtils.isNotEmpty(v)) {
                remarkSet.addAll(Arrays.asList(StringUtils.split(v,",")));
            }
        });
        List<String> resultList = new ArrayList<>(remarkSet);
        map.put("fieldGroup", resultList);
        map.put("remarkGroup", resultList);
        map.put("fieldGroupTotal", resultList.size());
//        // 读取文件
//        try {
//            String tempFilePath = "/worldlocation.json";
//            InputStream input = TTalentsBaseServiceImpl.class.getResourceAsStream(tempFilePath);
//            byte[] bytes = new byte[0];
//            bytes = new byte[input.available()];
//            input.read(bytes);
//            String str = new String(bytes);
//            // 转化JSON
//            //将字符串加载到jsonObject对象中
//            JSONObject jsonObject = JSON.parseObject(str);
//            Object fieldGroup = jsonObject.get("fieldGroup");
//            map.put("fieldGroup", fieldGroup);
//            Object remarkGroup = jsonObject.get("remarkGroup");
//            map.put("remarkGroup", remarkGroup);
//            List<Object> list = (List<Object>)(fieldGroup);
//            map.put("fieldGroupTotal", list.size());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        //添加论文发布趋势
        List<Map<String, Integer>> releaseTrend = tTalentsBaseDao.releaseTrend(tTalentsBase);
        for (int i = releaseTrend.size() - 1; 0 <= i; i--) {
            if (i > 0){
                int i1 = releaseTrend.get(i).get("time") - releaseTrend.get(i-1).get("time");
                if (i1 != 1){
                    for (int j = 1; j < i1 ;j++){
                        Map<String, Integer> map1 = new HashMap<>();
                        int i2 = releaseTrend.get(i - 1).get("time") + (i1-j);
                        map1.put("time",i2);
                        map1.put("value",0);
                        releaseTrend.add(i ,map1);
                    }
                }
            }
        }
        map.put("releaseTrend", releaseTrend);
        return map;
    }

    /**
     * 新增数据
     *
     * @param tTalentsBase 实例对象
     * @return 实例对象
     */
    @Override
    public TTalentsBase insert(TTalentsBase tTalentsBase) {
        this.tTalentsBaseDao.insert(tTalentsBase);
        return tTalentsBase;
    }

    /**
     * 修改数据
     *
     * @param tTalentsBase 实例对象
     * @return 实例对象
     */
    @Override
    public TTalentsBase update(TTalentsBase tTalentsBase) {
        this.tTalentsBaseDao.update(tTalentsBase);
        return this.queryById(tTalentsBase.getAuthorid());
    }

    /**
     * 通过主键删除数据
     *
     * @param authorid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long authorid) {
        return this.tTalentsBaseDao.deleteById(authorid) > 0;
    }
}
